package com.reggie.controller.admin;

import com.reggie.annotation.AutoFill;
import com.reggie.dto.CategoryDTO;
import com.reggie.dto.CategoryPageQueryDTO;
import com.reggie.entity.Category;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关的接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询接口")
    public R<PageResult> page( CategoryPageQueryDTO pageQueryDTO){
        log.info("分类开始分页：" + pageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(pageQueryDTO);
        return R.success(pageResult);
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public R<String> save(@RequestBody CategoryDTO categoryDTO){
        log.info("开启新增：" + categoryDTO);
        categoryService.save(categoryDTO);
        return R.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类接口")
    public R<String> update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类：" + categoryDTO);
        categoryService.update(categoryDTO);
        return R.success();
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除分类接口")
    public R<String> delete(Long id){
        log.info("删除菜品：" + id);
        categoryService.deleteById(id);
        return R.success();
    }

    /**
     * 启用禁用分类
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用分类接口")
    public R<String> startOrStop(@PathVariable Integer status,Long id){
        log.info("启用禁用的参数：{},{}"+ status,id);
        categoryService.startOrStop(status,id);
        return R.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public R<List<Category>>findByType(Integer type){
        log.info("根据类型查询分类: " + type);
        List<Category> categoryList = categoryService.findByType(type);
        return R.success(categoryList);
    }
}
